package scu.maqiang.visualization;
import javax.swing.*;

import scu.maqiang.mesh.Mesh2T3;
import scu.maqiang.mesh.T3Type;

import java.awt.*;
import java.io.IOException;

public class MeshProcessApp extends JPanel {
 
	public MeshProcessApp() {
		this.setBackground(Color.WHITE);
	}
	
	public void createMenu() {
		
	}
	

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
 
        Graphics2D g2d = (Graphics2D)g;
 
        g2d.translate(getWidth() / 2, getHeight() / 2);
        g2d.scale(1, -1);
//         g2d.setColor(Color.WHITE);
//        g2d.fillRect(-getWidth() / 2, -getHeight() / 2, getWidth(), getHeight());
//        g2d.setColor(Color.BLACK);
        g2d.drawLine(-getWidth() / 2, 0, getWidth() / 2, 0);
        g2d.drawLine(0, -getHeight() / 2, 0, getHeight()/2);
 //       g2d.fillRect(0, 0, 100, 100);

	    Mesh2T3 mesh = new Mesh2T3();
		mesh.square2D(5, 5, T3Type.Right);
	    double[][] xy = new double[3][2];
	    int[] xx = new int[4];
	    int[] yy = new int[4];
	    int scale = (int)(getHeight()/2 * 0.8);
	    for(int i = 0; i < mesh.getNt(); i++) {
	    	int[] ele = mesh.getElement(i);
	    	mesh.getCoordPerElement(ele, xy);
	    	xx[0] = (int)(xy[0][0] * scale);
	    	xx[1] = (int)(xy[1][0] * scale);
	    	xx[2] = (int)(xy[2][0] * scale);
	    	xx[3] = (int)(xy[0][0] * scale);
	    	yy[0] = (int)(xy[0][1] * scale);
	    	yy[1] = (int)(xy[1][1] * scale);
	    	yy[2] = (int)(xy[2][1] * scale);
	    	yy[3] = (int)(xy[0][1] * scale);
	    	//System.out.println(Arrays.toString(xx));
	    	g2d.drawPolyline(xx, yy, 4);
	    }
	    int[] xb = new int[2];
	    int[] yb = new int[2];
	    double[][] xyb = new double[2][2];
	    g2d.setColor(Color.RED);
	    for(int i = 0; i < mesh.getNb(); i++) {
	    	int[] ele = mesh.getBoundary(i);
	    	mesh.getCoordPerBoundary(ele, xyb);
	    	xb[0] = (int)(xyb[0][0] * scale);
	    	xb[1] = (int)(xyb[1][0] * scale);
	    	yb[0] = (int)(xyb[0][1] * scale);
	    	yb[1] = (int)(xyb[1][1] * scale);
	    	g2d.drawLine(xb[0], yb[0], xb[1], yb[1]);
	    }
    }
 
    private static void createAndShowGUI() {
        JFrame frame = new JFrame();
 
        // Add your component.
        frame.setContentPane(new MeshProcessApp());
 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.white);
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
 
    public static void main(String[] args) throws IOException {
        createAndShowGUI();
    }
}